
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册判断</title>
</head>
<body>
 
<%
    request.setCharacterEncoding("UTF-8");
    String user = new String(request.getParameter("username"));
    String pwd = new String(request.getParameter("password"));
    Class.forName("com.mysql.jdbc.Driver");
	String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8";
	String usename = "root";
	String psw= "123456";  
    Connection conn = DriverManager.getConnection(url,usename,psw);//得到连接  
    if(conn!=null){
    out.println("<script type='text/javascript'>alert('数据库连接成功！');</script>"); 
    PreparedStatement pStmt = conn.prepareStatement("select * from user where username = '" + user + "'");  
    ResultSet rs = pStmt.executeQuery();  			
	if(rs.next()){  
         out.println("<script type='text/javascript'>alert('该用户已存在，请重新注册！');window.location.href='register.jsp';</script>");  
    }
	else{  
          PreparedStatement tmt = conn.prepareStatement("Insert into user values('" + user + "','" + pwd + "')");  
          int rst = tmt.executeUpdate();  
          if (rst != 0){  
                out.println("<script type='text/javascript'>alert('用户注册成功！');window.location.href='login.jsp';</script>");    
          }
          else{  
                out.println("<script type='text/javascript'>alert('用户注册失败！');window.location.href='register.jsp';</script>");    
          }  
      }	
    }
	%>
 
 
</body>
</html>